package blog.service.impl;


import blog.dao.ChapterChapterLabelDao;
import blog.dao.ChapterLabelDao;
import blog.pojo.ChapterCategory;
import blog.pojo.ChapterChapterCategory;
import blog.pojo.ChapterChapterLabel;
import blog.pojo.ChapterLabel;
import blog.service.ChapterLabelService;
import blog.service.ChapterSpecialService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class ChapterLabelServiceImpl implements ChapterLabelService {

    @Autowired
    private ChapterChapterLabelDao chapterChapterLabelDao;

    @Autowired
    private ChapterLabelDao chapterLabelDao;

    @Override
    public List<ChapterLabel> getListByChapterId(Integer id) throws Exception {
        List<ChapterChapterLabel> chapterChapterLabelList = chapterChapterLabelDao.selectList(new QueryWrapper<>(new ChapterChapterLabel().setChapterId(id)));
        List<ChapterLabel> chapterLabelList = new ArrayList<>();
        for(ChapterChapterLabel chapterChapterLabel : chapterChapterLabelList){
            chapterLabelList.add(chapterLabelDao.selectById(chapterChapterLabel.getChapterLabelId()));
        }
        return chapterLabelList;
    }

    @Override
    public Page<ChapterLabel> getPage(Page<ChapterLabel> page, String keywords) throws Exception {
        QueryWrapper wrapper = null;
        if(keywords != null && !"".equals(keywords)){
            wrapper = new QueryWrapper<ChapterLabel>().like("name", keywords);
        }
        return chapterLabelDao.selectPage(page, wrapper);
    }

    public boolean edit(ChapterLabel chapterLabel) throws Exception{
        if(chapterLabel.getId() != null){
            return chapterLabelDao.updateById(chapterLabel) > 0;
        } else {
            return chapterLabelDao.insert(chapterLabel) > 0;
        }
    }

    @Override
    public boolean del(Integer id) throws Exception {
        if(chapterChapterLabelDao.selectCount(new QueryWrapper<ChapterChapterLabel>(new ChapterChapterLabel().setChapterLabelId(id))) > 0){
            return false;
        }
        return chapterLabelDao.deleteById(id) > 0;
    }

    @Override
    public List<ChapterLabel> getAll() throws Exception {
        return chapterLabelDao.selectList(null);
    }
}
